package com.enjoy.nettyhttp.server;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.*;
import io.netty.handler.ssl.SslContext;

/**
 * Created by HP on 2019/7/15.
 */
public class ServerHandlerInit extends ChannelInitializer<SocketChannel> {
    private final SslContext sslContext;

    public ServerHandlerInit(SslContext sslContext) {
        this.sslContext = sslContext;
    }

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline ph = socketChannel.pipeline();
       if (sslContext!=null){
           ph.addLast(sslContext.newHandler(socketChannel.alloc()));
       }
        ph.addLast("encoder", new HttpResponseEncoder())
                .addLast("decoder", new HttpRequestDecoder())
                .addLast("aggregator", new HttpObjectAggregator(10 * 1024 * 1024))
                .addLast("compressor", new HttpContentCompressor())
                .addLast("handler", new BusiHandler());

    }

}
